package controller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"mygo/app/models"
	"mygo/app/services"
	"mygo/app/tools"
	"strconv"
)

type foreCtl struct {
	db *gorm.DB
}

var Fore = foreCtl{}

func (ctl *foreCtl) GetForeDetailJson(c *gin.Context) {
	ctl.db = models.Db
	id := c.Query("id")
	t := c.Query("time")
	idInt, _ := strconv.Atoi(id)
	//能查到直接返回
	var foreDetail models.ForeDetailJson
	if ctl.db.Where("foreListId = ? AND time = ?", id, t).First(&foreDetail).RowsAffected > 0 {
		if foreDetail.State != "0" {
			tools.SuccessResp(c, "查询成功", foreDetail.Data)
			return
		}
	}
	//查不到则创建
	var foreList models.ForeList
	ctl.db.Where("id = ?", id).First(&foreList)
	var forecast = models.Forecast{}
	json.Unmarshal(foreList.Data, &forecast)
	//第一次创建进行预填处理

	defer func() {
		if err := recover(); err != nil {
			tools.SuccessResp(c, "查询成功", forecast)
		}
	}()
	if foreList.Code == "hx" {
		handelHx(&forecast, t, ctl.db)
	} else if foreList.Code == "df" {
		handelDf(&forecast, t, ctl.db)
	} else if foreList.Code == "hy" {
		handelHy(&forecast, t, ctl.db)
	} else if foreList.Code == "zb" {
		handelZb(&forecast, t, ctl.db)
	} else if foreList.Code == "sa" {
		services.HandelSa(&forecast, t, ctl.db)
	} else if foreList.Code == "sz" {
		services.HandelSz(&forecast, t, ctl.db)
	} else if foreList.Code == "dt" {
		//handelDt(&forecast, t, ctl.db)
		services.HandelDt(&forecast, t, ctl.db)
	} else if foreList.Code == "dtzb" {
		services.HandelDtzb(&forecast, t, ctl.db)
	} else if foreList.Code == "ydyl" {
		//handelYdyl(&forecast, t, ctl.db)
		services.HandelYdyl(&forecast, t, ctl.db)
	} else if foreList.Code == "fb" {
		handelFb(&forecast, t, ctl.db)
	} else if foreList.Code == "yy" {
		services.HandelYy(&forecast, t, ctl.db)
	} else if foreList.Code == "rahx" {
		services.HandelRahx(&forecast, t, ctl.db)
	} else if foreList.Code == "rayg" {
		services.HandelRayg(&forecast, t, ctl.db)
	} else if foreList.Code == "wzhy" {
		services.HandelWzhy(&forecast, t, ctl.db)
	} else if foreList.Code == "wzhx" {
		services.HandelWzhx(&forecast, t, ctl.db)
	} else if foreList.Code == "hsyc" {
		services.HandelHsyc(&forecast, t, ctl.db)
	} else if foreList.Code == "nj" {
		services.HandelNj(&forecast, t, ctl.db)
	} else if foreList.Code == "pyja" {
		services.HandelPyja(&forecast, t, ctl.db)
	} else if foreList.Code == "hdrc" {
		services.HandelHdrc(&forecast, t, ctl.db)
	} else if foreList.Code == "hyxian" {
		services.HandelHyxian(&forecast, t, ctl.db)
	} else if foreList.Code == "xhl" {
		services.HandelXhl(&forecast, t, ctl.db)
	}
	data, _ := json.Marshal(forecast)
	foreDetail = models.ForeDetailJson{
		ForeListId: uint(idInt),
		Time:       t,
		State:      "0",
		FileName:   "",
		Data:       data,
	}
	//ctl.db.Create(&foreDetail)
	ctl.db.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "idx_time_foreListId"}},
		DoUpdates: clause.AssignmentColumns([]string{"data", "state", "fileName"}),
	}).Create(&foreDetail)
	tools.SuccessResp(c, "查询成功", forecast)
}

// GetForeDetailLast
func (ctl *foreCtl) GetForeDetailLastJson(c *gin.Context) {
	ctl.db = models.Db
	id := c.Query("id")
	t := c.Query("time")
	//能查到直接返回
	var foreDetail models.ForeDetailJson
	if ctl.db.Where("foreListId = ? AND time < ? AND state != ?", tools.StrToInt(id), t, "0").Order("time desc").First(&foreDetail).RowsAffected > 0 {
		tools.SuccessResp(c, "查询成功", gin.H{
			"forecast": foreDetail.Data,
			"time":     foreDetail.Time,
		})
		return
	}
	//查不到返回空
	tools.SuccessResp(c, "查询成功", nil)
}

// SubmitForeData
func (ctl *foreCtl) SubmitForeDataJson(c *gin.Context) {
	var err error
	ctl.db = models.Db
	id := c.Query("id")
	t := c.Query("time")
	var req models.Forecast
	c.ShouldBind(&req)
	var foreDetail models.ForeDetailJson
	if ctl.db.Where("foreListId = ? AND time = ?", id, t).First(&foreDetail).RowsAffected > 0 {
		foreDetail.State = "1"
		foreDetail.Data, err = json.Marshal(req)
		if err != nil {
			tools.ErrorResp(c, 500, "提交失败")
			return
		}
		ctl.db.Save(&foreDetail)
		tools.SuccessResp(c, "提交成功", nil)
		return
	}
	tools.ErrorResp(c, 500, "提交失败")
}

// GetForeTable
func (ctl *foreCtl) GetForeTableJson(c *gin.Context) {
	ctl.db = models.Db
	id := c.Query("id")
	t := c.Query("time")
	var foreDetail models.ForeDetailJson
	ctl.db.Where("foreListId = ? AND time = ?", id, t).First(&foreDetail)
	tools.SuccessResp(c, "获取成功", foreDetail)
}

// UpdateForeJson
func (ctl *foreCtl) UpdateForeJson(c *gin.Context) {
	var err error
	ctl.db = models.Db
	id := c.Query("id")
	t := c.Query("time")
	state := c.Query("state")
	fileName := c.Query("fileName")
	var foreDetail models.ForeDetailJson
	ctl.db.Where("foreListId = ? AND time = ?", id, t).Find(&foreDetail)
	//foreDetail.State = state
	if state != "" {
		foreDetail.State = state
	}
	if fileName != "" {
		foreDetail.FileName = fileName
	}
	err = ctl.db.Save(&foreDetail).Error
	if err != nil {
		tools.ErrorResp(c, 500, "修改失败")
		return
	}
	tools.SuccessResp(c, "修改成功", nil)
}
